package com.hyb.controller;


import com.alibaba.fastjson.JSON;
import com.hyb.entity.Contacts;
import com.hyb.page.PageReq;
import com.hyb.response.Result;
import com.hyb.service.ContactsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  客户联系人Controller
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/contacts")
public class ContactsController {

    @Autowired
    private ContactsService contactsService;

    @RequestMapping(value = "/createContact",method = RequestMethod.POST)
    @ApiOperation("新建客户联系人")
    public String createContact(@RequestBody Contacts contacts){
        contactsService.createContacts(contacts);
        return JSON.toJSONString(new Result().setCode(200).setMessage("保存成功"));
    }

    @RequestMapping(value = "/loadcontactByCustomerId",method = RequestMethod.GET)
    @ApiOperation("根据客户Id加载客户联系人")
    public String loadcontactByCustomerId(Integer limit,Integer current,Integer customerId){
        PageReq pageReq = contactsService.loadContactsByCustomerId(limit, current, customerId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(pageReq));
    }

    @RequestMapping(value = "/loadContactByContactId",method = RequestMethod.GET)
    @ApiOperation("根据联系人Id加载联系人")
    public String loadContactByContactId(Integer contactId){
         Contacts contactByContactId = contactsService.getContactByContactId(contactId);
         return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(contactByContactId));
    }

    @RequestMapping(value = "/updateContact",method = RequestMethod.PUT)
    @ApiOperation("修改联系人信息")
    public String updateContact(@RequestBody Contacts contacts){
        contactsService.updateContact(contacts);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }

    @RequestMapping(value = "/deleteContact",method = RequestMethod.DELETE)
    @ApiOperation("删除联系人信息")
    public String deleteContact(Integer contactId){
        contactsService.deleteContactByContactId(contactId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("删除成功"));
    }
}
